package com.qf.listen;

import com.qf.utils.EmailUtils;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA. User: Administrator Date: 2021/11/16 0016 Time: 17:22 Description: No
 * Description
 */
@Component
public class InformationListen {

  @Autowired RedisTemplate redisTemplate;

  @Autowired EmailUtils emailUtils;

  protected static Logger logger= LoggerFactory.getLogger(InformationListen.class);

  @RabbitListener(queues = "qfshop-email-queue")
  public void sendEmail(Map map, Channel channel, Message message) {
      logger.info("进入到了邮箱发送的消费者："+map);
      try{
          //从map中获取到邮箱
          String email = map.get("email").toString();
          Map send = emailUtils.send(email);
          redisTemplate.opsForValue().set(email,send.get("code").toString());
          //设置验证码的存活时长5分钟
          redisTemplate.expire(email,300, TimeUnit.SECONDS);
          //手动确认确认该条信息消费成功
          channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
          logger.info("发送成功");
      }catch (Exception e){
        logger.info("发送失败 case{}"+e.getMessage());
      }

  }
}
